//
//  GQHPasswordSettingController.m
//  Xanthium
//
//  Created by GuanQinghao on 24/02/2018.
//  Copyright © 2018 GuanQinghao. All rights reserved.
//

#pragma mark Macro
#import "GQHHeader.h"

#pragma mark Model
#import "GQHUserModel.h"

#pragma mark Object

#pragma mark View
#import "GQHPasswordSettingView.h"

#pragma mark Controller
#import "GQHPasswordSettingController.h"
#import "GQHLoginController.h"

#pragma mark CocoaPods

#pragma mark ----------Header End----------

@interface GQHPasswordSettingController () <GQHPasswordSettingViewDelegate> {
    
#pragma mark Data
    
#pragma mark View
    /// 自定义根视图
    GQHPasswordSettingView *rootView;
}

@end


@implementation GQHPasswordSettingController

#pragma mark --Lifecycle
- (void)loadView {
    DLog();
    
    rootView = [[GQHPasswordSettingView alloc] initWithFrame:[UIScreen mainScreen].bounds];
    rootView.qh_delegate = self;
    self.view = rootView;
    
}

- (void)viewDidLoad {
    [super viewDidLoad];
    DLog();
    
    // 快速添加视图背景图
    self.view.layer.contents = (id)[UIImage imageNamed:@"login_background"].CGImage;
}

- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
    DLog();
    
    self.qh_titleLabel.text = @"设置登录密码";
    self.qh_navigationBar.backgroundColor = kClearColor;
}

- (void)viewDidAppear:(BOOL)animated {
    [super viewDidAppear:animated];
    DLog();
}

#pragma mark --Delegate
- (void)qh_setupPassword:(NSString *)password again:(NSString *)again {
    
    if ([password isEqualToString:again]) {
        
        NSMutableDictionary *parameters = [NSMutableDictionary dictionary];
        [parameters setValue:self.qh_mobile forKey:@"phone"];
        [parameters setValue:password forKey:@"passWord"];
        [parameters setValue:again forKey:@"password1"];
        
        if (_qh_isRegistered) {
            
            [GQHUserModel qh_setPasswordWith:parameters success:^(BOOL status, NSNumber *code, NSString *message, GQHUserModel *user) {
                
                if (status) {
                    
                    GQHLoginController *loginController = [[GQHLoginController alloc] init];
                    [self.navigationController pushViewController:loginController animated:YES];
                } else {
                    
                    [self showToastWithText:message];
                }
            } failure:^(NSError *error) {
                
                NSString *errorString = [NSString stringWithFormat:@"网络出错:%@,code:%ld", error.domain, (long)error.code];
                [self showToastWithText:errorString];
            }];
        } else {
            
            [GQHUserModel qh_changePasswordWith:parameters success:^(BOOL status, NSNumber *code, NSString *message) {
                
                if (status) {
                    
                    GQHLoginController *loginController = [[GQHLoginController alloc] init];
                    [self.navigationController pushViewController:loginController animated:YES];
                } else {
                    
                    [self showToastWithText:message];
                }
            } failure:^(NSError *error) {
                
                NSString *errorString = [NSString stringWithFormat:@"网络出错:%@,code:%ld", error.domain, (long)error.code];
                [self showToastWithText:errorString];
            }];
        }
    } else {
        
        [self qh_alertWithTitle:nil message:@"两次密码输入不一致，请重新输入" actionTitle:nil handler:nil completion:nil];
    }
}

#pragma mark --TargetMethod
- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event {
    
    // 全局统一收起键盘
    [[[UIApplication sharedApplication] keyWindow] endEditing:YES];
}
#pragma mark --PrivateMethod

#pragma mark --Setter

#pragma mark --Getter

#pragma mark --View

@end
